<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/11/20
 * Time: 下午3:05
 */
class Runner extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        $this->load->helper('url_helper');
        $this->load->helper('url');
        $this->load->model('users_model');
        $this->load->model('areas_model','areas');
    }

    /**
     * @return object
     */
    public function sb()
    {
        is_upgrade($this->db,569);
        return;

    }

    // 所有可以晋升的经理，
    public function upgrade_list()
    {
        $sql = "SELECT * FROM manager WHERE identity IN ('合作销售经理','大客户经理','特聘大客户经理','资深大客户经理','大区经理','高级大区经理') AND job_date IS NOT NULL;";
        $manager_list = $this->db->query($sql)->result_array();
        foreach ($manager_list as $key=>$manager)
        {
            $manager_id = $manager['id'];
            if(!is_upgrade($this->db,$manager_id))
            {
                continue;
            }
            $res = check_upgrade($this->db,$manager_id);
            if($res)
            {
                if($res['res'] == 1)
                {
                    // 可以晋升
                    $now_identity = $manager['identity'];
                    $next_identity = next_identity($manager['identity'])[0];
                    // 如果有父级，存入奖金 push_money
                    $parent_id = '';
                    $push_money = 0;
                    if($manager['is_bind'] == '已绑定')
                    {
                        $sql = "SELECT parent_id FROM manager_bind WHERE manager_id = ".$manager_id." limit 1";
                        $parent_id = $this->db->query($sql)->row_array()['parent_id'];
                        $push_money = next_identity($manager['identity'])[1];
                    }
                    $sql = "SELECT * FROM upgrade WHERE manager_id = $manager_id AND manager_from = '$now_identity' AND manager_to = '$next_identity' AND type = '可晋升' limit 1";
                    $re = $this->db->query($sql)->row_array();
                    if($re)
                    {
                        if($re['status'] == '待审核')
                        {
                            $this->db->delete('upgrade',array('id'=>$re['id']));
                            $data['manager_id'] = $manager_id;
                            $data['parent_id'] = $parent_id;
                            $data['push_money'] = $push_money;
                            $data['manager_from'] = $now_identity;
                            $data['manager_to'] = $next_identity;
                            $data['status'] = '待审核';
                            $data['type'] = '可晋升';
                            $data['created_at'] = date('Y-m-d H:i:s',time());
                            $bool = $this->db->insert("upgrade",$data);
                            $insert_id = $this->db->insert_id();
                            log_message('error',"新增晋升列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                        }
                    }else{
                        $data['manager_id'] = $manager_id;
                        $data['parent_id'] = $parent_id;
                        $data['push_money'] = $push_money;
                        $data['manager_from'] = $now_identity;
                        $data['manager_to'] = $next_identity;
                        $data['status'] = '待审核';
                        $data['type'] = '可晋升';
                        $data['created_at'] = date('Y-m-d H:i:s',time());
                        $bool = $this->db->insert("upgrade",$data);
                        $insert_id = $this->db->insert_id();
                        log_message('error',"新增晋升列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                    }
                }
            }
        }
    }
    // 申请转正列表
    function become_list(){
        $sql = "SELECT * FROM manager WHERE identity IN ('试合作销售经理','大客户经理','资深大客户经理','大区经理','高级大区经理','销售总监') AND job_date IS NULL;";
        $manager_list = $this->db->query($sql)->result_array();
        foreach ($manager_list as $key=>$manager)
        {
            $manager_id = $manager['id'];
            $m_info = $this->users_model->get_one($manager_id);
            if(!$m_info['job_date'])
            {
                $entry_date = $m_info['entry_date'] ? $m_info['entry_date'] :  date("Y-m-d", strtotime($m_info['created_at']));
                $now_date = date("Y-m-d",time());
                $relax = [$entry_date];
                if($entry_date == $now_date)
                {
                    // 当天注册的
                    $relax[] = date("Y-m-d", strtotime("+1 months", strtotime($entry_date)));
                }
                while (strtotime($entry_date) < strtotime($now_date))
                {
                    $entry_date = date("Y-m-d", strtotime("+1 months", strtotime($entry_date)));
                    $relax[] = $entry_date;
                }
                // 取出倒数四个
                $relax = array_slice(array_reverse($relax),-4);
                $t1 = isset($relax[0]) ? $relax[0] : '';
                $t2 = isset($relax[1]) ? $relax[1] : '';
                $t3 = isset($relax[2]) ? $relax[2] : '';
                $t4 = isset($relax[3]) ? $relax[3] : '';
                $res = check_become($this->db,$manager_id,$t1,$t2,$t3,$t4);

                if($res)
                {
                    if($res['res'] == 1)
                    {
                        // 可以转正
                        $now_identity = $manager['identity'];
                        $next_identity = become_identity($manager['identity']);
                        // 如果有父级，存入奖金 push_money
                        $parent_id = '';
                        $push_money = 0;
                        if($manager['is_bind'] == '已绑定')
                        {
                            $sql = "SELECT parent_id FROM manager_bind WHERE manager_id = ".$manager_id." limit 1";
                            $parent_id = $this->db->query($sql)->row_array()['parent_id'];
                        }
                        $sql = "SELECT * FROM upgrade WHERE manager_id = $manager_id AND manager_from = '$now_identity' AND manager_to = '$next_identity' AND type = '可转正' limit 1";
                        $re = $this->db->query($sql)->row_array();
                        if($re)
                        {
                            if($re['status'] == '待审核')
                            {
                                $this->db->delete('upgrade',array('id'=>$re['id']));
                                $data['manager_id'] = $manager_id;
                                $data['parent_id'] = $parent_id;
                                $data['push_money'] = $push_money;
                                $data['manager_from'] = $now_identity;
                                $data['manager_to'] = $next_identity;
                                $data['status'] = '待审核';
                                $data['type'] = '可转正';
                                $data['created_at'] = date('Y-m-d H:i:s',time());
                                $bool = $this->db->insert("upgrade",$data);
                                $insert_id = $this->db->insert_id();
                                log_message('error',"新增转正列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                            }
                        }else{
                            $data['manager_id'] = $manager_id;
                            $data['parent_id'] = $parent_id;
                            $data['push_money'] = $push_money;
                            $data['manager_from'] = $now_identity;
                            $data['manager_to'] = $next_identity;
                            $data['status'] = '待审核';
                            $data['type'] = '可转正';
                            $data['created_at'] = date('Y-m-d H:i:s',time());
                            $bool = $this->db->insert("upgrade",$data);
                            $insert_id = $this->db->insert_id();
                            // 更新manager 表的 eligible_date
                            $this->db->where(array("id"=>$manager_id))->update("manager",array("eligible_date"=>date('Y-m-d',time())));
                            log_message('error',"新增转正列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                        }
                    }
                }

            }
        }

    }
    // 转正后和转正前的降级列表 cj
    function lowgrade_list(){
        $sql = "SELECT * FROM manager WHERE identity IN ('合作销售经理','特聘大客户经理','大客户经理','资深大客户经理','大区经理','高级大区经理','销售总监');";
        $manager_list = $this->db->query($sql)->result_array();
        foreach ($manager_list as $key=>$manager)
        {
            $manager_id = $manager['id'];
            $m_info = $manager;
            // 未转正的降级
            if(!$m_info['job_date'])
            {
                // 只对入职日期足月的操作
                $entry_date = $m_info['entry_date'] ? $m_info['entry_date'] :  date("Y-m-d", strtotime($m_info['created_at']));
                $now_date = date("Y-m-d",time());
                $now_day = date('d');
                $entry_day = substr($entry_date,-2);
//                $three_month_ago = date("Y-m-d", strtotime("-3 months", strtotime($now_date)));
                if($now_day != $entry_day)
                {
                    continue;
                }
                $relax = [$entry_date];
                while (strtotime($entry_date) < strtotime($now_date))
                {
                    $entry_date = date("Y-m-d", strtotime("+1 months", strtotime($entry_date)));
                    $relax[] = $entry_date;
                }
                // 降级要考察三个月的情况，三个月不能转正则视作降级
                if(sizeof($relax) < 4)
                {
                    continue;
                }
                // 取出倒数四个
                list($t1,$t2,$t3,$t4) = array_slice(array_reverse($relax),-4);
                $res = check_lowgrade($this->db,$manager_id,$t1,$t2,$t3,$t4);

                if($res)
                {
                    if($res['res'] == 1)
                    {
                        // 可以降级
                        $now_identity = $manager['identity'];
                        $next_identity = low_identity($manager['identity']);
                        // 如果有父级
                        $parent_id = '';
                        $push_money = 0;
                        if($manager['is_bind'] == '已绑定')
                        {
                            $sql = "SELECT parent_id FROM manager_bind WHERE manager_id = ".$manager_id." limit 1";
                            $parent_id = $this->db->query($sql)->row_array()['parent_id'];
                        }
                        $sql = "SELECT * FROM upgrade WHERE manager_id = $manager_id AND manager_from = '$now_identity' AND manager_to = '$next_identity' AND type = '可降级' limit 1";
                        $re = $this->db->query($sql)->row_array();
                        if($re)
                        {
                            if($re['status'] == '待审核')
                            {
                                $this->db->delete('upgrade',array('id'=>$re['id']));
                                $data['manager_id'] = $manager_id;
                                $data['parent_id'] = $parent_id;
                                $data['push_money'] = $push_money;
                                $data['manager_from'] = $now_identity;
                                $data['manager_to'] = $next_identity;
                                $data['status'] = '待审核';
                                $data['type'] = '可降级';
                                $data['created_at'] = date('Y-m-d H:i:s',time());
                                $bool = $this->db->insert("upgrade",$data);
                                $insert_id = $this->db->insert_id();
                                log_message('error',"新增转正列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                            }
                        }else{
                            $data['manager_id'] = $manager_id;
                            $data['parent_id'] = $parent_id;
                            $data['push_money'] = $push_money;
                            $data['manager_from'] = $now_identity;
                            $data['manager_to'] = $next_identity;
                            $data['status'] = '待审核';
                            $data['type'] = '可降级';
                            $data['created_at'] = date('Y-m-d H:i:s',time());
                            $bool = $this->db->insert("upgrade",$data);
                            $insert_id = $this->db->insert_id();
                            // 更新manager 表的 eligible_date
                            $this->db->where(array("id"=>$manager_id))->update("manager",array("eligible_date"=>date('Y-m-d',time())));
                            log_message('error',"新增降级列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                        }
                    }
                }

            }else{
                // 转正后的降级
                // 按自然月，本月最后一天统计
                $firstday = date('Y-m-01', strtotime(date('Y-m-d H:i:s')));
                $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
                if(date('Y-m-d',time()) == $lastday && is_lowgrade($this->db,$manager_id))
                {
                    $res = check_lowgrade_job($this->db,$manager_id);

                    if($res)
                    {
                        if($res['res'] == 1)
                        {
                            // 可以降级
                            $now_identity = $manager['identity'];
                            $next_identity = low_identity($manager['identity']);
                            // 如果有父级
                            $parent_id = '';
                            $push_money = 0;
                            if($manager['is_bind'] == '已绑定')
                            {
                                $sql = "SELECT parent_id FROM manager_bind WHERE manager_id = ".$manager_id." limit 1";
                                $parent_id = $this->db->query($sql)->row_array()['parent_id'];
                            }
                            $sql = "SELECT * FROM upgrade WHERE manager_id = $manager_id AND manager_from = '$now_identity' AND manager_to = '$next_identity' AND type = '可降级' limit 1";
                            $re = $this->db->query($sql)->row_array();
                            if($re)
                            {
                                if($re['status'] == '待审核')
                                {
                                    $this->db->delete('upgrade',array('id'=>$re['id']));
                                    $data['manager_id'] = $manager_id;
                                    $data['parent_id'] = $parent_id;
                                    $data['push_money'] = $push_money;
                                    $data['manager_from'] = $now_identity;
                                    $data['manager_to'] = $next_identity;
                                    $data['status'] = '待审核';
                                    $data['type'] = '可降级';
                                    $data['created_at'] = date('Y-m-d H:i:s',time());
                                    $bool = $this->db->insert("upgrade",$data);
                                    $insert_id = $this->db->insert_id();
                                    log_message('error',"新增转正列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                                }
                            }else{
                                $data['manager_id'] = $manager_id;
                                $data['parent_id'] = $parent_id;
                                $data['push_money'] = $push_money;
                                $data['manager_from'] = $now_identity;
                                $data['manager_to'] = $next_identity;
                                $data['status'] = '待审核';
                                $data['type'] = '可降级';
                                $data['created_at'] = date('Y-m-d H:i:s',time());
                                $bool = $this->db->insert("upgrade",$data);
                                $insert_id = $this->db->insert_id();
                                // 更新manager 表的 eligible_date
                                $this->db->where(array("id"=>$manager_id))->update("manager",array("eligible_date"=>date('Y-m-d',time())));
                                log_message('error',"新增降级列表数据，ID：".$insert_id.'，插入是否成功：'.$bool);
                            }
                        }
                    }
                }
//                $end_month_day
            }
        }
    }

    // 月底的奖金计算
    function deal_push_money(){

//        $this->deal_push_money_help(525,'2017-11',0.02);
        // 按自然月，本月最后一天统计
        $firstday = date('Y-m-01', strtotime(date('Y-m-d H:i:s')));
        $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
        if(date('Y-m-d',time()) != $lastday)
        {
            echo '奖金在月底结算，本月不是当月最后一天';
            return;
        }

        $sql = "SELECT * FROM manager";
        $manager_list = $this->db->query($sql)->result_array();
        $date =  date('Y-m',time());
        $last_date = date('Y-m', strtotime("-1 month",time()));

        foreach ($manager_list as $key => $manager)
        {
            $identity = $manager['identity'];
            switch ($identity)
            {
                case '试合作销售经理' :
                    $bonus = $this->deal_push_money_help($manager['id'],$date,0.05);
                    $this->add_income($manager['id'],$date,$bonus);
                    break;
                case '合作销售经理' :
                    $bonus = $this->deal_push_money_help($manager['id'],$date,0.05);
                    $this->add_income($manager['id'],$date,$bonus);
                    break;
                case '特聘大客户经理' :
                    $bonus = $this->deal_push_money_help($manager['id'],$date,0.05);
                    $this->add_income($manager['id'],$date,$bonus);
                    break;
                case '资深大客户经理' :
                    $bonus = $this->deal_push_money_help($manager['id'],$date,0.04);
                    $this->add_income($manager['id'],$date,$bonus);
                    break;
                case '大客户经理' :
                    $bonus = $this->deal_push_money_help($manager['id'],$date,0.04);
                    $this->add_income($manager['id'],$date,$bonus);
                    break;
                case '大区经理' :
                    if($manager['job_date'])
                    {
                        // 已经转正
                        $bonus = $this->deal_push_money_help($manager['id'],$date,0.02);
                        $this->add_income($manager['id'],$date,$bonus);
                    }else{
                        // 未转正
                        $mid = $manager['id'];
                        // 本月业绩
                        $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$date'; ";
                        $p1 = $this->db->query($sql)->row_array()['pm'];
                        // 本月晋升奖金
                        $sql2 = "SELECT up.parent_id, up.created_at, IFNULL(sum(up.push_money),0) as total_money FROM upgrade AS up WHERE up.status = '审核成功' AND parent_id = $mid";
                        $p2 = $this->db->query($sql2)->row_array()['total_money'];
                        // 上月业绩
                        $sql3 = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$last_date'; ";
                        $p3 = $this->db->query($sql3)->row_array()['pm'];
                        $p = 0;
                        if($p1 > $p3){
                            $p = ($p1-$p3)*0.01;
                        }
                        $bonus = round($p+$p2,2);
                        $this->add_income($manager['id'],$date,$bonus);
                    }
                    break;

                case '高级大区经理' :
                    if($manager['job_date'])
                    {
                        // 已经转正
                        $bonus = $this->deal_push_money_help($manager['id'],$date,0.02);
                        $this->add_income($manager['id'],$date,$bonus);
                    }else{
                        // 未转正
                        $mid = $manager['id'];
                        // 本月业绩
                        $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$date'; ";
                        $p1 = $this->db->query($sql)->row_array()['pm'];
                        // 本月晋升奖金
                        $sql2 = "SELECT up.parent_id, up.created_at, IFNULL(sum(up.push_money),0) as total_money FROM upgrade AS up WHERE up.status = '审核成功' AND parent_id = $mid";
                        $p2 = $this->db->query($sql2)->row_array()['total_money'];
                        // 上月业绩
                        $sql3 = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$last_date'; ";
                        $p3 = $this->db->query($sql3)->row_array()['pm'];
                        $p = 0;
                        if($p1 > $p3){
                            $p = ($p1-$p3)*0.01;
                        }
                        $bonus = round($p+$p2,2);
                        $this->add_income($manager['id'],$date,$bonus);
                    }
                    break;

                case '销售总监' :
                    if($manager['job_date'])
                    {
                        // 已经转正
                        $bonus = $this->deal_push_money_help($manager['id'],$date,0.02);
                        $this->add_income($manager['id'],$date,$bonus);
                    }else{
                        // 未转正
                        $mid = $manager['id'];
                        // 本月业绩
                        $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$date'; ";
                        $p1 = $this->db->query($sql)->row_array()['pm'];
                        // 本月晋升奖金
                        $sql2 = "SELECT up.parent_id, up.created_at, IFNULL(sum(up.push_money),0) as total_money FROM upgrade AS up WHERE up.status = '审核成功' AND parent_id = $mid";
                        $p2 = $this->db->query($sql2)->row_array()['total_money'];
                        // 上月业绩
                        $sql3 = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$last_date'; ";
                        $p3 = $this->db->query($sql3)->row_array()['pm'];
                        $p = 0;
                        if($p1 > $p3){
                            $p = ($p1-$p3)*0.01;
                        }
                        $bonus = round($p+$p2,2);
                        $this->add_income($manager['id'],$date,$bonus);
                    }
                    break;
                default :
                    break;
            }


        }
    }

    // 奖金计算辅助
    private function deal_push_money_help($mid,$date,$bili){
//        SELECT ifnull(sum(a.money), 0)
//        INTO first_month_performance
//        FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = mid) AS a
//        WHERE a.created_at >= t2 AND a.created_at < t1;

        $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE date_format(a.created_at,'%Y-%m') = '$date'; ";
        $p1 = $this->db->query($sql)->row_array()['pm'];
        $sql2 = "SELECT up.parent_id, up.created_at, IFNULL(sum(up.push_money),0) as total_money FROM upgrade AS up WHERE up.status = '审核成功' AND parent_id = $mid";
        $p2 = $this->db->query($sql2)->row_array()['total_money'];
        $p = round($p1*$bili+$p2,2);
        return $p;

    }

    // 修改income表
    private function add_income($mid,$date,$bonus){
        $rs = $this->db->where(array('manager_id'=>$mid,'month'=>$date))->get("income")->row_array();
        $manager_info = $this->db->where(array('id'=>$mid))->get("manager")->row_array();
        $wage = $manager_info['wage'];
        if($rs){
            // 更新
            $id = $rs['id'];
            $res = $this->db->where(array("id"=>$id))->update("income",array("wage"=> $wage,"bonus"=>$bonus));
            log_message('error', '更新income表，ID： '.$id.'，奖金：'.$bonus.'，工资：'.$wage.'，更新结果：'.$res.'，更新时间：'.time());
        }else{
            // 插入
            $data_arr['manager_id'] = $mid;
            $data_arr['wage'] = $wage;
            $data_arr['performance_wage'] = 0;
            $data_arr['bonus'] = $bonus;
            $data_arr['commissions'] = 0;
            $data_arr['month'] = $date;
            $res = $this->db->insert("income",$data_arr);
            $insert_id = $this->db->insert_id();
            log_message('error', '新增income表，ID： '.$insert_id.'，奖金：'.$bonus.'，工资：'.$wage.'，插入结果：'.$res.'，插入时间：'.time());
        }

    }

    // 大区经理，高级大区经理，销售总监未转正时候 业绩增长评分 修改score表
    function addUpScore(){
        $sql = "SELECT * FROM manager WHERE identity in ('大区经理','高级大区经理','销售总监') AND job_date IS NULL";
        $manager_list = $this->db->query($sql)->result_array();
        foreach ($manager_list as $key=>$manager)
        {
            $entry_date = $manager['entry_date'] ? $manager['entry_date'] :  date("Y-m-d", strtotime($manager['created_at']));
            // 本月业绩
            $now_day = date('d');
            $entry_day = substr($entry_date,-2);
//                $three_month_ago = date("Y-m-d", strtotime("-3 months", strtotime($now_date)));
            if($now_day != $entry_day)
            {
                continue;
            }
            // 本月的业绩
            $mid = $manager['id'];
            $date = date("Y-m-d",time()).'23:59:59';
            $last_date = date("Y-m-d H:i:s", strtotime("-1 months", strtotime($date)));
            $last_last_date = date("Y-m-d H:i:s", strtotime("-2 months", strtotime($date)));
            $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE a.created_at <= '$date' AND a.created_at >= '$last_date'; ";
            $p1 = $this->db->query($sql)->row_array()['pm'];
            // 上个月的业绩
            if(date("Y-m-d",time()) == date("Y-m-d", strtotime("+1 months", strtotime($entry_date))))
            {
                // 首月默认10000
                $p2 = '10000';
            }else{
                $sql = "SELECT IFNULL(sum(a.money),0) as pm FROM (SELECT * FROM push_money AS pm WHERE pm.manager_id = $mid GROUP BY order_id UNION ALL SELECT * FROM push_money WHERE parent_id = $mid) AS a WHERE a.created_at <= '$last_date' AND a.created_at >= '$last_last_date'; ";
                $p2 = $this->db->query($sql)->row_array()['pm'];
                $p2 = $p2 == 0 ? 1 : $p2;
            }
            // 计算评分
            if($p1 - $p2 > 0)
            {
                $score = round(($p1 - $p2)/$p2*3,2);
                if($score > 0)
                {
                    $this->db->insert("score",array('manager_id'=>$mid,'score'=>$score,'reason'=>'业绩增长评分','created_at'=>date("Y-m-d H:i:s")));
                }
            }
        }
    }

}